package cn.com.cennavi.bean;

import java.util.ArrayList;
import java.util.HashMap;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.LineSegment;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.Point;

import cn.com.cennavi.util.MapUtil;
import java.io.Serializable;

public class GPSBean implements Serializable {
	/**
	 * @Fields serialVersionUID : TODO（用一句话描述这个变量表示什么）
	 */
	 /**
	 * @Fields serialVersionUID : TODO（用一句话描述这个变量表示什么）
	 */
	private static final long serialVersionUID = 1L;
	public double lon;
	public double lat;
	public double speed;
	public long timeStamp;
	public double dir;

	/**
	 * gpsData:11659848:4003424:102:1451579837:178|11659922:4003390:95:
	 * 1451579840:178|11659981:4003361:97:1451579843:178|11660038:4003328:94:
	 * 1451579846:178|
	 **/
	public ArrayList<GPSBean> getGPSBean(String gpsData) {
		ArrayList<GPSBean> dataList = new ArrayList<>();
		String[] gpssArr = gpsData.split("\\|");
		for (String gpsline : gpssArr) {
			String[] gpsArr = gpsline.split("\\:");
			GPSBean gps = new GPSBean();
			gps.lon = Double.parseDouble(gpsArr[0]);
			gps.lat = Double.parseDouble(gpsArr[1]);
			gps.speed = Double.parseDouble(gpsArr[2]);
			gps.timeStamp = Long.parseLong(gpsArr[3]);
			gps.dir = Double.parseDouble(gpsArr[4]);
			dataList.add(gps);
		}
		return dataList;

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String line = "11659848:4003424:102:1451579837:178|11659922:4003390:95:1451579840:178|11659981:4003361:97:1451579843:178|11660038:4003328:94:1451579846:178|";
		ArrayList<GPSBean> list = new GPSBean().getGPSBean(line);
	}


	private String getDisFromSnode(double lon, double lat, LineString lineString) {
		// TODO Auto-generated method stub

		Point point = MapUtil.getPoint(new Coordinate(lon, lat));
		Coordinate cs = MapUtil.closestPoint(point, lineString);

		Point prjp = MapUtil.getPoint(cs);

		Coordinate coordinates[] = lineString.getCoordinates();
		int size = coordinates.length - 1;

		double distance = 0, prjDistanceFormSNode = 0;
		/** 计算投影点到gps点的距离 **/
		double mindis = MapUtil.calPointDistance(lon, lat, prjp.getCoordinate().x, prjp.getCoordinate().y);
		for (int i = 0; i < size; i++) {

			distance = MapUtil.calPointDistance(coordinates[i], coordinates[i + 1]);

			LineSegment lineSegment = new LineSegment(coordinates[i], coordinates[i + 1]);
			Coordinate cur = MapUtil.closestPoint2LineSegment(prjp.getCoordinate(), lineSegment);
			if (equal(prjp.getCoordinate(), cur)) {
				distance = MapUtil.calPointDistance(coordinates[i], prjp.getCoordinate());
				prjDistanceFormSNode = prjDistanceFormSNode + distance;
				break;
			}

			prjDistanceFormSNode = prjDistanceFormSNode + distance;
		}

		return (int) mindis + "," + (int) prjDistanceFormSNode;
	}

	boolean equal(Coordinate a, Coordinate b) {
		return (Math.abs(a.x - b.x) < 0.0000001) && (Math.abs(a.y - b.y) < 0.0000001);
	}

	/**
	 * 获取link对应的形状点
	 */
	private ArrayList<String> getLinksGEO(String linkAttr, HashMap<Integer, String> mapData) {
		// TODO Auto-generated method stub
		ArrayList<String> dataList = new ArrayList<>();
		String[] links = linkAttr.split("\\|");
		for (int i = 0; i < links.length; i++) {
			if (mapData.get(Integer.parseInt(links[i])) != null) {
				String linkGEO = mapData.get(Integer.parseInt(links[i]));
				// System.out.println("找到"+links[i]+"的形状点");
				dataList.add(linkGEO);
			} else {
				// System.out.println("沒有找到" + links[i] + "的形状点");
				return null;
			}
		}
		return dataList;
	}

	public ArrayList<Integer> getNewGpsProjAttr(String linkAttr, ArrayList<String> newGpsProjAttr, HashMap<Integer, String> linkLengthData, HashMap<Integer, LinkNode> linkDisMap) {
		ArrayList<Integer> lengthList = new ArrayList<>();
		for (int j = 0; j < newGpsProjAttr.size(); j++) {
			String[] newGps = newGpsProjAttr.get(j).split(",");// 1467867,17
			/** gps点距离分歧点的长度=该点距离投影link末尾的距离+该条link距离分歧点的距离 **/
			/** 该点距离投影link末尾的距离=该条link长度-投影点距离link起始点的距离 **/
			// int gpsLinkDis =
			// Integer.parseInt(linkLengthData.get(Integer.parseInt(newGps[0])))
			// - Integer.parseInt(newGps[1]);
			int dis = linkDisMap.get(Integer.parseInt(newGps[0])).leftDis - Integer.parseInt(newGps[1]);
			// int dis = gpsLinkDis + link_disMap.get(newGps[0]);
			lengthList.add(dis);
		}
		return lengthList;
	}

}

